<?php

function smarty_function_file_upload($params, &$smarty)
{
	if(!isset($params["field"])) throw new Kohana_Exception ("file_upload: param 'field' is required!");
	
	$optional_params = array("filename", "max_size", "width", "height", "uploaded_function");
	
	$input  = "<input type=\"hidden\" id=\"{$params["field"]}\" name=\"{$params["field"]}\" value=\"".(isset($params["filename"])?$params["filename"]:"")."\" />\n";
	$input .= "<img id=\"img_{$params["field"]}\" src=\"".(isset($params["filename"])?Kohana::$base_url.$params["filename"]:"")."\" />\n";
	$input .= "<iframe id=\"iframe_file\" src=\"".Kohana::$base_url."file/upload?field={$params["field"]}";
	foreach($optional_params as $param){
		if(array_key_exists($param, $params)){
			$input .= "&$param={$params[$param]}";
		}
	}
	$input .= "\" width=\"100%\" height=\"25\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\"></iframe>\n";
	
	return $input;
}